<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Shop;
use App\Models\ShopCom;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class ShopController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('店铺众筹管理')
            ->description('店铺众筹管理')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shop);
        $grid->model()->where('status', '<>', 0)->orderBy('id', 'desc')->with('shopcrowd');
        $grid->id('Id');
        $grid->shop_type('店铺类型')->display(function ($value) {
            switch ($value) {
                case '1':
                    return "<span class='label label-warning'>线上</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>线下</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->shop_name('店铺名称')->expand(function ($model) {
            $comments = $model->where('id', $this->id)->get()->map(function ($comment) {
                $comment->crowd_funding_start = date('Y-m-d', $comment->crowd_funding_start);
                return $comment->only(['id', 'promoter', 'address', 'phone', 'wechat', 'area', 'crowd_funding_start']);;
            });

            return new Table(['ID', '发起人', '店铺地址', '手机号', '微信', '面积(m²)', '发起时间'], $comments->toArray());
        });
        $grid->shop_price('总金额')->editable();
        $grid->column('已众筹金额')->display(function () {
            if (count($this->shopcrowd)) {
                return $this->shopcrowd->sum('money');
            } else {
                return '0.00';
            }
        })->expand(function () {
            $datas = $this->shopcrowd->map(function ($data, $key) {
                $data->key = $key + 1;
                return $data->only(['key', 'phone', 'money', 'created_at']);
            });
            return new Table(['序号', '手机号', '金额', '认购时间'], $datas->toArray());
        });

        $grid->shop_info('店铺图片')->display(function ($value) {
            return $value[0];
        })->gallery(['width' => 50]);
        $grid->status('状态')->display(function ($value) {
            switch ($value) {
                case '0':
                    return "<span class='label label-warning'>待审核</span>";
                    break;
                case '1':
                    return "<span class='label label-danger'>众筹中</span>";
                    break;
                case '2':
                    return "<span class='label label-primary'>未通过</span>";
                    break;
                case '3':
                    return "<span class='label label-success'>众筹成功</span>";
                    break;
                case '4':
                    return "<span class='label label-danger'>众筹失败</span>";
                    break;

                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');

        $grid->disableCreateButton();

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->scope('线上店铺')->where('shop_type', 1);
            $filter->scope('线下店铺')->where('shop_type', 2);
            $filter->like('shop_name', '店铺名称');
            $filter->like('phone', '手机号');
            $filter->equal('status','店铺状态')->radio([
                '' => '全部',
                1 => '众筹中',
                2 => '申请未通过',
                3 => '众筹成功',
                4 => '众筹失败',
            ]);

            $filter->between('created_at', '申请时间')->datetime();

        });
        
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            if ($actions->row->status == 3) {
                $actions->append("<a href='/admin/shopdetail/" . $actions->getKey() . "'>" . '<button type="button" class="btn btn-primary">店铺商品</button>' . "</a>");
            }
        });

        return $grid;
    }
    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑店铺')
            ->body($this->form()->edit($id));
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Shop);


        
        $form->multipleImage('shop_info', '图片')->move('images/shop/' . date("Ym/d", time()))->uniqueName()->sortable()->removable()->help('请框选图片或按住ctrl键选择多张图片,第一张图为默认封面图');


        return $form;
    }

    //店铺详情
    public function shopdetail(Request $request, $id)
    {
        $lists = ShopCom::where('shop_id', $id)->with(['commodity', 'commodityAttr','brand'])->get();
        $shop_name = Shop::where('id', $id)->value('shop_name');

        $lists->transform(function ($item, $key) {
            $item->key = $key + 1;
            $item->com_name = $item->commodity->commodity_name;
            $item->brand_name = $item->brand->name;
            $item->com_attr = $item->commodityAttr->attr_value;
            $item->is_buy = ($item->is_buy) ? "<span class='label label-success'>是</span>" : "<span class='label label-danger'>否</span>";
            $item->status = ($item->status == 1) ? "<span class='label label-success'>是</span>" : "<span class='label label-danger'>否</span>";
            $item->edit = "<a href='/admin/shopcoms/" . $item->id . "/edit'><i class='fa fa-edit'></i></a><a href='/admin/delcom/" . $item->id . "'><i class='fa fa-trash'></i></a>";
            // $item->del = "<a href='javascript:void(0);' data-id=' .$item->id.' class='grid-row-delete'><i class='fa fa-trash'></i></a>";
            return $item->only(['key', 'com_name', 'brand_name', 'com_attr', 'stock', 'is_buy', 'price', 'status', 'edit']);
        });
        $headers = ['序号', '商品名', '品牌','属性', '库存', '换购', '换购价格', '上架', '操作'];
        $table = new Table($headers, $lists->toArray());

        $box = new Box($shop_name . '的店铺商品', $table->render());
        $box->style('primary');
        $box->solid();
        $box->back();
        $content = new Content();
        return $content
            ->header('店铺商品')
            ->description('详情')
            ->body($box->render());
    }

    public function shopList()
    {
        $list = Shop::where(['status' => 3])->get(['id', 'shop_name as text']);

        return $list->toArray();
    }

}
